<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\ServiceConfig;
use Illuminate\Support\Facades\Auth;
use App\Repositories\ServiceConfigRepository;
use App\Http\Requests\Admin\ServiceConfig\ShowRequest;
use App\Http\Requests\Admin\ServiceConfig\QueryResource;
use App\Http\Requests\Admin\ServiceConfig\ServiceConfigCreate;
use App\Http\Requests\Admin\ServiceConfig\ServiceConfigUpdate;
use App\Http\Requests\Admin\ServiceConfig\ServiceConfigDestroy;

class ServiceConfigController extends Controller
{
    protected $serviceConfigRepository;

    /**
     * ServiceConfigsController constructor.
     * @param ServiceConfigRepository $serviceConfigRepository
     */
    public function __construct(
        ServiceConfigRepository $serviceConfigRepository
    )
    {
        $this->serviceConfigRepository = $serviceConfigRepository;
    }

    /**
     * 获取服务站配置列表资源列表
     *
     * Date: 2018/05/10
     * @author Tenny
     * @param QueryResource $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(QueryResource $request)
    {
        $service_id = $request->get('area_id', false);
        $limit = $request->get('paginate', false);
        $name = $request->get('name', false);
        $phone = $request->get('phone', false);
        $serviceConfigs = $this->serviceConfigRepository->getServiceConfigs($service_id, $limit, $name, $phone);
        return success($serviceConfigs);
    }

    /**
     * 获取服务站配置详情
     *
     * Date: 2018/05/09
     * @author Tenny
     * @param ShowRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(ShowRequest $request, ServiceConfig $serviceConfig)
    {
        return success($serviceConfig);
    }


    /**
     * 创建服务站配置
     *
     * Date: 2018/05/09
     * @author Tenny
     * @param ServiceConfigCreate $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\GeneralException
     */
    public function store(ServiceConfigCreate $request)
    {
        $attributes = $this->extractInputFromRules($request, $request->rules());
        $serviceConfigs = $this->serviceConfigRepository->create($attributes);
        return stored($serviceConfigs);
    }

    /**
     * 更新服务站配置资源
     *
     * Date: 2018/05/11
     * @author Tenny
     * @param ServiceConfigUpdate $request
     * @param serviceConfig $serviceConfig
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(ServiceConfigUpdate $request, ServiceConfig $serviceConfig)
    {
        /*
         * $table->unsignedInteger('service_id')->index()->comment('服务站id');
            $table->string('to_work')->nullable()->comment('上班时间');
            $table->string('off_work')->nullable()->comment('下班时间');
            $table->decimal('first_weight', 10, 2)->default(0.00)->comment('快递员每单费用');
            $table->decimal('first_weight_price', 10, 2)->default(0.00)->comment('服务站费用');
            $table->decimal('continued_weight', 10, 2)->default(0.00)->comment('自助服务网点提成');
            $table->decimal('continued_weight_price', 10, 2)->default(0.00)->comment('取件员超重费用');
            $table->decimal('continued_weight_price', 10, 2)->default(0.00)->comment('服务站超重费用');
         * */
        $serviceConfigsAttr = $request->only([
            'service_id',
            'to_work',
            'off_work',
            'first_weight',
            'first_weight_price',
            'continued_weight',
            'status',
        ]);
        $serviceConfig->update($serviceConfigsAttr);
        return updated($serviceConfig);
    }

    /**
     * 删除 服务站配置 资源
     *
     * Date: 2018/05/09
     * @author Tenny
     * @param ServiceConfigDestroy $request
     * @param ServiceConfig $serviceConfig
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function destroy(ServiceConfigDestroy $request, ServiceConfig $serviceConfig)
    {
        $force = $request->get('force');
        $force ? $serviceConfig->delete() : $serviceConfig->forceDelete();
        return deleted();
    }
}
